#! /usr/bin/env python
# ripframes

import os
import sys
from libtovid import rip
from libtovid.media import MediaFile


if __name__ == '__main__':
    # Get args
    if len(sys.argv) < 3:
        print "Usage: ripframes FRAMES video.avi"
        print "Where FRAMES is a single frame, range of frames or 'all'"
        print "(default) to rip all frames."
        print "For example: ripframes 20-30 video.avi"
        sys.exit()
    else:
        frames = sys.argv[1]
        filename = sys.argv[2]
        
    print "Ripping frames %s from file: %s" % (frames, filename)
    
    video = MediaFile(filename)
    tempdir = 'frames_%s' % filename
    if frames == 'all':
        range = 'all'
    elif frames.find('-'):
        start, end = frames.split('-')
        range = [int(start), int(end)]
    else:
        range = int(frames)
    outfiles = rip.rip_frames(video, tempdir, range)
    print "Successfully ripped %s image files to: %s" % \
          (len(outfiles), tempdir)

"""
for FILE in $@; do
    NAVFILE="$FILE.nav"
    PREFIX=$(echo "$FILE" | sed 's/\.mpg$//')
    echo "Ripping frames from $FILE..."
    tcdemux -f 29.970 -W -i "$FILE" > "$NAVFILE"
    transcode -i "$FILE" -c $FRAMES --nav_seek "$NAVFILE" -y im -F png -o "frames/$PREFIX"
    sleep 1s
    rm "$NAVFILE"
done
"""
